package com.dalitek.sampleface;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.dalitek.sampleface.q._Q1V1Activity;
import com.dalitek.sampleface.q._Q1VNActivity;
import com.oliveapp.libcommon.utility.LogUtil;

import java.util.ArrayList;

public class LaunchActivity extends BaseActivity {
    private static final String TAG = LaunchActivity.class.getSimpleName();
    private ArrayList<Object> requestPermissions;
    private static final int REQUEST_CODE = 1;
    private AlertDialog alertDialog;
    private TextView tvWelcome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch_activiy);
        //打开LOG打印等级
        LogUtil.MIN_LOG_LEVEL = -1;
        LogUtil.ENABLE_LOG = true;
        LogUtil.e(TAG, "onCreate");

        //设置全屏
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        initView();
        doInit();

    }

    private void initView() {
        tvWelcome = findViewById(R.id.tv_welcome);
        findViewById(R.id.tv_welcome).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doInit();
            }
        });
    }

    // 申请权限相关
    private static String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
    };

    //检查权限
    private boolean checkPermission() {

        boolean flag = true;
        requestPermissions = new ArrayList<>();

        if (ContextCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_DENIED) {
            requestPermissions.add(permissions[0]);
            flag = false;
            toast("请打开存储读写权限，确保APP正常运行");
        }
        if (ContextCompat.checkSelfPermission(this, permissions[1]) == PackageManager.PERMISSION_DENIED) {
            requestPermissions.add(permissions[1]);
            flag = false;
            toast("请打开存储读写权限，确保APP正常运行");
        }
        if (ContextCompat.checkSelfPermission(this, permissions[2]) == PackageManager.PERMISSION_DENIED) {
            requestPermissions.add(permissions[2]);
            flag = false;
            toast("没有摄像头权限我什么都做不了哦!");
        }
        return flag;
    }

    private void doInit(){
        if(!checkPermission()){
            ActivityCompat.requestPermissions(this, requestPermissions.toArray(new String[requestPermissions.size()]), REQUEST_CODE);
        }else{
            tvWelcome.setText("点击我进行选择...");
            showLoadingDialog();
            LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(bContext).inflate(R.layout.dialog_select_activity,null);
            linearLayout.findViewById(R.id.btn_1v1).setOnClickListener(view -> {
                startActivity(new Intent(bContext,_1V1Activity.class));
                finish();
            });
            linearLayout.findViewById(R.id.btn_1vn).setOnClickListener(view -> {
                startActivity(new Intent(bContext, _1VNActivity.class));
                finish();
            });
            linearLayout.findViewById(R.id.btn_nvn).setOnClickListener(view -> {
                startActivity(new Intent(bContext,_NVNActivity.class));
                finish();
            });
            linearLayout.findViewById(R.id.btn_1vn_q).setOnClickListener(view -> {
                startActivity(new Intent(bContext, _Q1VNActivity.class));
                finish();
            });
            linearLayout.findViewById(R.id.btn_1v1_q).setOnClickListener(view -> {
                startActivity(new Intent(bContext, _Q1V1Activity.class));
                finish();
            });
             alertDialog = new AlertDialog.Builder(this).setTitle("请选择")
                    .setView(linearLayout)
                    .setPositiveButton("返回", (paramAnonymousDialogInterface, paramAnonymousInt) -> {

                    }).create();
            dismissLoadingDialog();
            alertDialog.show();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        dismissLoadingDialog();
        if(alertDialog!=null && alertDialog.isShowing()){
            alertDialog.dismiss();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean isGranted = true;
        try {
            switch (requestCode) {
                case REQUEST_CODE:
                    for (int i : grantResults) {
                        if (i == PackageManager.PERMISSION_DENIED) {
                            toast("请授予相应权限，确保APP正常运行");
                            isGranted = false;
                            break;
                        }
                    }
                    break;
            }
            if (isGranted) {
                doInit();
            }
        } catch (Exception e) {
            LogUtil.e(TAG, "Failed to request Permission", e);
        }
    }
}